﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace Agiltastic
{
    public partial class fClub : FormBase
    {
        private ClubDao _clubDao = new ClubDao();
        private List<Club> _listClub = new List<Club>();
        private Club _currentSelected;
      
        public fClub()
        {
            InitializeComponent();
            try
            {
                LoadGrid();
                EnableControls(false);
                CurrentMode = Mode.DoNothing;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
        }

        private void LoadGrid()
        {
            _listClub = _clubDao.GetAllClub();
            dgvClub.DataSource = _listClub;
        }

        private void EnableControls(bool b)
        {
            txtName.Enabled = b;
            txtAdress.Enabled = b;
            txtEmail.Enabled = b;
            txtWebSite.Enabled = b;
            btnSave.Enabled = b;
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            dgvClub.DataSource = _listClub.FindAll(c => c.Name.ToUpperInvariant().Contains(txtFilter.Text.ToUpperInvariant()));
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtAdress.Text = "";
            txtEmail.Text = "";
            txtWebSite.Text = "";
            _currentSelected = new Club();
            CurrentMode = Mode.Add;
            EnableControls(true);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvClub.SelectedCells.Count > 0)
            {
                var name = (String)dgvClub.SelectedCells[1].Value;
                if (name != "")
                {
                    _currentSelected = _listClub.Find(c => c.Name == name);
                    txtName.Text = _currentSelected.Name;
                    txtAdress.Text = _currentSelected.Adress;
                    txtEmail.Text = _currentSelected.Email;
                    txtWebSite.Text = _currentSelected.WebSite;
                    EnableControls(true);
                    CurrentMode = Mode.Edit;
                }
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtName.Text))
                MessageBox.Show("The name cannot be empty");
            else
            {
                _currentSelected.Name = txtName.Text;
                _currentSelected.Adress = txtAdress.Text;
                _currentSelected.Email = txtEmail.Text;
                _currentSelected.WebSite = txtWebSite.Text;
                if (CurrentMode == Mode.Edit)
                {
                    _clubDao.Update(_currentSelected);
                }

                if (CurrentMode == Mode.Add)
                {
                   _clubDao.Save(_currentSelected);
                }
                LoadGrid();
                EnableControls(false);
            }
        }

    }
}
